﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Reviewer.Entities.Services;
using Reviewer.Business;
using Reviewer.Entities;

public partial class CMSV2_Controls_Game_GamePegiRating : System.Web.UI.UserControl
{
    public Guid GameId { get; set; }

    private Format[] formats;
    public Format[] Formats
    {
        get { return this.formats; }
        set
        {
            this.formats = value;
            this.Rebind();
        }
    }

    private void Rebind()
    {
        this.allRatings = null;
        this.uiRepFormats.DataSource = this.formats;
        this.uiRepFormats.DataBind();
    }



    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
        }
    }




    protected void uiPEGIAgeRating_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton senderButton = sender as ImageButton;
        if (senderButton != null)
        {
            foreach (Control c in senderButton.Parent.Controls)
            {
                if (c is ImageButton && (c as ImageButton).ID.StartsWith("uiIbPEGI"))
                {
                    (c as ImageButton).Width = 25;
                    (c as ImageButton).BorderWidth = 0;
                }
            }

            senderButton.BorderWidth = 0;
            senderButton.Width = 45;
        }
    }

    protected void uiIbRating_Click(object sender, ImageClickEventArgs e)
    {
        (sender as ImageButton).BorderWidth = (sender as ImageButton).BorderWidth.Value > 0 ? 0 : 0;
        (sender as ImageButton).Width = (sender as ImageButton).Width.Value > 30 ? 30 : 50;
    }

    protected void uiBtnSave_Click(object sender, EventArgs e)
    {
        List<PEGIRating> ratings = new List<PEGIRating>();
        foreach (RepeaterItem item in this.uiRepFormats.Items)
        {
            HiddenField hf = item.FindControl("uiHfFormatId") as HiddenField;

            PEGIRating rating = new PEGIRating();
            rating.GameId = this.GameId;
            rating.Format = int.Parse(hf.Value);
            rating.AgeRating = 0;

            foreach (Control c in item.Controls)
            {
                if (!(c is ImageButton))
                    continue;

                ImageButton btn = c as ImageButton;

                switch (btn.ID)
                {
                    case "uiIbPEGI3":
                        if (btn.Width.Value > 30)
                            rating.AgeRating = 3;
                        break;
                    case "uiIbPEGI7":
                        if (btn.Width.Value > 30)
                            rating.AgeRating = 7;
                        break;
                    case "uiIbPEGI12":
                        if (btn.Width.Value > 30)
                            rating.AgeRating = 12;
                        break;
                    case "uiIbPEGI16":
                        if (btn.Width.Value > 30)
                            rating.AgeRating = 16;
                        break;
                    case "uiIbPEGI18":
                        if (btn.Width.Value > 30)
                            rating.AgeRating = 18;
                        break;
                    case "uiIbRatingLanguage":
                        rating.IsRatedBadLanguage = (btn.Width.Value > 30);
                        break;
                    case "uiIbRatingDiscrimination":
                        rating.IsRatedDiscrimination = (btn.Width.Value > 30);
                        break;
                    case "uiIbRatingDrugs":
                        rating.IsRatedDrugs = (btn.Width.Value > 30);
                        break;
                    case "uiIbRatingFear":
                        rating.IsRatedFear = (btn.Width.Value > 30);
                        break;
                    case "uiIbRatingGambling":
                        rating.IsRatedGambling = (btn.Width.Value > 30);
                        break;
                    case "uiIbRatingSex":
                        rating.IsRatedSex = (btn.Width.Value > 30);
                        break;
                    case "uiIbRatingViolence":
                        rating.IsRatedViolence = (btn.Width.Value > 30);
                        break;
                    case "uiIbRatingOnline":
                        rating.IsRatedOnline = (btn.Width.Value > 30);
                        break;
                }
            }

            ratings.Add(rating);
        }

        ServiceFactory.GameAdminService.SetRatings(this.GameId, ratings);

        /*
        using (IGameAdminService adminService = ServiceFactory.GameAdminService)
        using (IGameService service = ServiceFactory.GameService)
        {
            PEGIRating rating = service.GetRating(this.GameId);
            if (rating == null)
            {
                rating = new PEGIRating();
                rating.GameId = this.GameId;
            }

            rating.AgeRating = this.uiIbPEGI3.BorderWidth.Value > 0 ? 3
                : this.uiIbPEGI7.BorderWidth.Value > 0 ? 7
                : this.uiIbPEGI12.BorderWidth.Value > 0 ? 12
                : this.uiIbPEGI16.BorderWidth.Value > 0 ? 16
                : this.uiIbPEGI18.BorderWidth.Value > 0 ? 18 : 0;

            rating.IsRatedBadLanguage = this.uiIbRatingLanguage.BorderWidth.Value > 0;
            rating.IsRatedDiscrimination = this.uiIbRatingDiscrimination.BorderWidth.Value > 0;
            rating.IsRatedDrugs = this.uiIbRatingDrugs.BorderWidth.Value > 0;
            rating.IsRatedFear = this.uiIbRatingFear.BorderWidth.Value > 0;
            rating.IsRatedGambling = this.uiIbRatingGambling.BorderWidth.Value > 0;
            rating.IsRatedOnline = this.uiIbRatingOnline.BorderWidth.Value > 0;
            rating.IsRatedSex = this.uiIbRatingSex.BorderWidth.Value > 0;
            rating.IsRatedViolence = this.uiIbRatingViolence.BorderWidth.Value > 0;

            adminService.SetRating(rating);
        }
         * */
    }

    private ICollection<PEGIRating> allRatings;

    protected void uiRepFormats_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            HiddenField hf = e.Item.FindControl("uiHfFormatId") as HiddenField;
            if (allRatings == null)
            {
                allRatings = ServiceFactory.GameService.GetRatings(this.GameId);
            }

            if (allRatings != null)
            {
                PEGIRating rating = allRatings.FirstOrDefault(t => t.Format.Equals(int.Parse(hf.Value)));
                if (rating != null)
                {
                    ImageButton ageRating = null;
                    switch (rating.AgeRating)
                    {
                        case 3:
                            ageRating = e.Item.FindControl("uiIbPEGI3") as ImageButton;
                            break;
                        case 7:
                            ageRating = e.Item.FindControl("uiIbPEGI7") as ImageButton;
                            break;
                        case 12:
                            ageRating = e.Item.FindControl("uiIbPEGI12") as ImageButton;
                            break;
                        case 16:
                            ageRating = e.Item.FindControl("uiIbPEGI16") as ImageButton;
                            break;
                        case 18:
                            ageRating = e.Item.FindControl("uiIbPEGI18") as ImageButton;
                            break;

                    }
                    if (ageRating != null)
                    {
                        uiPEGIAgeRating_Click(ageRating, null);
                    }

                    if (rating.IsRatedBadLanguage)
                        uiIbRating_Click(e.Item.FindControl("uiIbRatingLanguage"), null);
                    if (rating.IsRatedDiscrimination)
                        uiIbRating_Click(e.Item.FindControl("uiIbRatingDiscrimination"), null);
                    if (rating.IsRatedDrugs)
                        uiIbRating_Click(e.Item.FindControl("uiIbRatingDrugs"), null);
                    if (rating.IsRatedFear)
                        uiIbRating_Click(e.Item.FindControl("uiIbRatingFear"), null);
                    if (rating.IsRatedGambling)
                        uiIbRating_Click(e.Item.FindControl("uiIbRatingGambling"), null);
                    if (rating.IsRatedSex)
                        uiIbRating_Click(e.Item.FindControl("uiIbRatingSex"), null);
                    if (rating.IsRatedViolence)
                        uiIbRating_Click(e.Item.FindControl("uiIbRatingViolence"), null);
                    if (rating.IsRatedOnline)
                        uiIbRating_Click(e.Item.FindControl("uiIbRatingOnline"), null);
                }
            }
        }
    }
}
